from django.urls import path, re_path, reverse
from django.utils.safestring import mark_safe
from stark.service.s1 import StarkHandler, choice_display, m2m_display, SearchOption
from app_main.forms.for_stark import StudentModelForm


class StudentHandler(StarkHandler):
    model_form_class = StudentModelForm

    def display_score(self, obj=None, is_head=None, *args, **kwargs):
        if is_head:
            return '积分'

        re_url = reverse('stark:app_main_scorerecord_list', args=(obj.id,))
        return mark_safe('<a target="blank" href="%s">%s</a>' % (re_url, obj.score))

    list_display = [
        'customer',
        'qq',
        'mobile',
        'emergency_contact',
        m2m_display('班级', 'class_list'),
        display_score,
        choice_display('状态', 'student_status'),
        StarkHandler.display_edit
    ]

    has_add_btn = False

    def get_urls(self):
        patterns = [
            path('list/', self.wrapper(self.list_view), name=self.get_list_url_name),
            re_path('edit/(?P<nid>\d+)/$', self.wrapper(self.edit_view), name=self.get_edit_url_name),
        ]

        patterns.extend(self.extra_urls())

        return patterns

    search_group = [
        SearchOption('class_list')
    ]
